/**
 * 
 */
package Clients;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.net.tftp.TFTP;
import org.apache.commons.net.tftp.TFTPClient;

import org.apache.log4j.Logger;

import ProtoSimulator.Status;

/**
 * @author jjmb
 *
 */
public class Tftp extends Thread {
    private static final String CLASS_NAME = Tftp.class.getName();
    private static int maxRequests = 0;
    private static List requestedFileList = null;
    private static String tftpResponse = null;
    private static long requestedElapsedTime = 0;
    private static Iterator i;
    private Status status;
    private TFTPClient tftpc = new TFTPClient();
    private boolean writeFileLocally = false;
    
    public static Logger logger = Logger.getLogger(CLASS_NAME);
    
    /** Creates a new instance of Tftp */
    public Tftp(List fileList, int requests) {
        requestedFileList = fileList;
        maxRequests = requests;
    }
    
    /** Creates a new instance of Tftp */
    public Tftp(Status s, List fileList, int requests) {
    	status = s;
        requestedFileList = fileList;
        maxRequests = requests;
    }
    
    public void run() {
        int requestCount = 0;
        i = requestedFileList.iterator();
        
        while(requestCount < maxRequests) {
            long r0 = System.currentTimeMillis();
            while(i.hasNext()) {
                try {
                } catch(Exception e){
                    logger.fatal(e);
                }
                long r1 = System.currentTimeMillis();
                requestedElapsedTime = r1 - r0;
                logger.info("Request("+ this.getName()  +") #" + requestCount + " processed, took " + requestedElapsedTime +"ms");
                requestCount++;
            }
        }
    }
}